package com.ruoyi.web.controller.tool;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.OperatorType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SysClientData;
import com.ruoyi.system.service.ISysClientDataService;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 产线运行记录Controller
 * 
 * @author spark
 * @date 2023-08-19
 */
@ApiOperation("产线运行记录")
@RestController
@RequestMapping("/api/data")
@Anonymous
public class TestClientDataController extends BaseController
{
    @Autowired
    private ISysClientDataService sysClientDataService;

    /**
     * 查询产线运行记录列表
     */
    @ApiOperation("查询产线运行记录列表")
    @GetMapping("/list")
    public TableDataInfo list(SysClientData sysClientData)
    {
        startPage();
        List<SysClientData> list = sysClientDataService.selectSysClientDataList(sysClientData);
        return getDataTable(list);
    }


    /**
     * 获取产线运行记录详细信息
     */
    @ApiOperation("获取产线运行记录详细信息")
    @GetMapping(value = "/{dataId}")
    public AjaxResult getInfo(@PathVariable("dataId") Long dataId)
    {
        return success(sysClientDataService.selectSysClientDataByDataId(dataId));
    }

    /**
     * 新增产线运行记录
     */
    @Log(title = "产线运行记录", businessType = BusinessType.OTHER ,operatorType= OperatorType.CLIENT)
    @ApiOperation("新增产线运行记录")
    @PostMapping
    public AjaxResult add(@RequestBody SysClientData sysClientData)
    {
        if(null == sysClientData.getDurationTime()){
            return new AjaxResult(-1, "持续时间不能为空", null);
        }
        if(null == sysClientData.getClientId() || StringUtils.isBlank(sysClientData.getDataState())){
            return new AjaxResult(-1, "必填项不能为空", null);
        }
        Integer durationTime = sysClientData.getDurationTime();
        Calendar c = Calendar.getInstance();
        sysClientData.setDataStartTime(c.getTime());
        c.add(Calendar.SECOND,durationTime);
        sysClientData.setDataEndTime(c.getTime());
        return toAjax(sysClientDataService.insertSysClientDataByUplaod(sysClientData));
    }

}
